package com.integrationpath.mengine.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.compass.core.Compass;
import org.compass.core.CompassException;
import org.compass.core.CompassHits;
import org.compass.core.CompassQuery;
import org.compass.core.CompassSession;
import org.compass.core.CompassTransaction;

import com.integrationpath.mengine.model.Article;
import com.integrationpath.mengine.model.Link;
import com.integrationpath.mengine.service.WebServicesManager;

public class WebServicesManagerImpl implements WebServicesManager {
	   
	public Compass compass;
	
	public Compass getCompass() {
		return compass;
	}

	public void setCompass(Compass compass) {
		this.compass = compass;
	}

	/*
	 * Get a finite number of articles order by date.
	 */
	public List<Article> getArticles() {
		CompassSession session = compass.openSession();
		CompassHits hits = null;
		String query = "+status:1";
		CompassTransaction tx = null;
		try {
			tx = session.beginTransaction();

			hits = session.queryBuilder().queryString(query)
					.toQuery()
					// .addSort("country",CompassQuery.SortPropertyType.STRING)
					.hits();

			tx.commit();
		} catch (CompassException ce) {
			if (tx != null)
				tx.rollback();
		} finally {
			session.close();
		}
		
		return prepareResponse(hits);
	}

	/**
	 * Get the links for the BLOGS widget.
	 * @author bciurdea@gmail.com, 07/06/2010 @ 01:27 am
	 * @return List&lt;Link&gt;
	 */
	public List<Link> getBlogs() {
		CompassSession session = compass.openSession();
		CompassHits hits = null;
		String query = "$/linkCompass/linkCategory/linkType/id:2";
		CompassTransaction tx = null;
		try {
			tx = session.beginTransaction();

			hits = session.queryBuilder().queryString(query).toQuery()
			.addSort("$/linkCompass/linkCategory/id", CompassQuery.SortPropertyType.STRING)
			.hits();

			tx.commit();
		} catch (CompassException ce) {
			if (tx != null)
				tx.rollback();
		} finally {
			session.close();
		}
		
		return prepareLinkResponse(hits);
	}
	
	/**
     * Get the links for the RESOURCES widget.
     * @author bciurdea@gmail.com, 09/06/2010 @ 01:50 am
     * @return List&lt;Link&gt;
     */
	public List<Link> getResources() {
		CompassSession session = compass.openSession();
		CompassHits hits = null;
		String query = "$/linkCompass/linkCategory/linkType/id:3";
		CompassTransaction tx = null;
		try {
			tx = session.beginTransaction();

			hits = session.queryBuilder().queryString(query).toQuery()
			.addSort("$/linkCompass/linkCategory/id", CompassQuery.SortPropertyType.STRING)
			.hits();

			tx.commit();
		} catch (CompassException ce) {
			if (tx != null)
				tx.rollback();
		} finally {
			session.close();
		}
		
		return prepareLinkResponse(hits);
	}
	
	private List<Article> prepareResponse(CompassHits hits) {
		List<Article> articles  = new ArrayList<Article>();
		if(hits != null)
			for (int i = 0; i < hits.length(); i++) {
				Article article = (Article) hits.data(i);
				article.setContent(null);
				articles.add(article);
			}
		
		return articles;
	}
	
	/**
	 * Prepares the links list.
	 * @author bciurdea@gmail.com, 07/06/2010 @ 02:02 am
	 * @param hits CompassHits
	 * @return List&lt;Link&gt;
	 */
	private List<Link> prepareLinkResponse(CompassHits hits) {
		List<Link> links  = new ArrayList<Link>();
		if(hits != null)
			for (int i = 0; i < hits.length(); i++) {
				Link link = (Link) hits.data(i);
				links.add(link);
			}
		
		return links;
	}

}
